using System;
using System.Drawing;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Forms;
using System.Windows.Markup;
using Common;
using Common.Enum;
using CustomChart;
using device;
using Infragistics.UltraChart.Data.Series;
using Infragistics.UltraChart.Resources.Appearance;
using SExperiment;

namespace MainView.MainControls
{
	public partial class UCTemperatureChart : System.Windows.Controls.UserControl, IDisposable, IComponentConnector
	{
		private TimeChart _Chart;

		public static readonly DependencyProperty TemperatureControlModeProperty = DependencyProperty.Register("TemperatureControlMode", typeof(ETemperatureControlMode), typeof(UCTemperatureChart), new UIPropertyMetadata(ETemperatureControlMode.Module, OnTemperatureControlModeChanged));

		public static readonly DependencyProperty UseHotBonnetProperty = DependencyProperty.Register("UseHotBonnet", typeof(bool), typeof(UCTemperatureChart), new UIPropertyMetadata(true, OnUseHotBonnetChanged));

		public static readonly DependencyProperty ExperimentProperty = DependencyProperty.Register("Experiment", typeof(Experiment), typeof(UCTemperatureChart), new UIPropertyMetadata(null, OnExperimentChanged));

		private bool _UpdateMaxX = true;

		protected XYSeriesEx _HotlidTemperatureSeries = new XYSeriesEx();

		protected XYSeriesEx _BlockTemperatureSeries = new XYSeriesEx();

		protected XYSeriesEx _TubeTemperatureSeries = new XYSeriesEx();

		protected XYSeriesEx[] _SubTubeTemperatureSeries = new XYSeriesEx[8];

		private int interval = 1;

		protected double singlePix = 2.0;

		public bool AccuracyDown;

		public TimeChart Chart
		{
			get
			{
				if (windowsFormsHost.Child == null)
				{
					_Chart = new TimeChart();
					_Chart.Chart.Dock = DockStyle.Fill;
					windowsFormsHost.Child = _Chart.Chart;
				}
				return _Chart;
			}
		}

		public ETemperatureControlMode TemperatureControlMode
		{
			get
			{
				return (ETemperatureControlMode)GetValue(TemperatureControlModeProperty);
			}
			set
			{
				SetValue(TemperatureControlModeProperty, value);
			}
		}

		public bool UseHotBonnet
		{
			get
			{
				return (bool)GetValue(UseHotBonnetProperty);
			}
			set
			{
				SetValue(UseHotBonnetProperty, value);
			}
		}

		public Experiment Experiment
		{
			get
			{
				return (Experiment)GetValue(ExperimentProperty);
			}
			set
			{
				SetValue(ExperimentProperty, value);
			}
		}

		public bool UpdateMaxX
		{
			get
			{
				return _UpdateMaxX;
			}
			set
			{
				_UpdateMaxX = value;
			}
		}

		private static void OnTemperatureControlModeChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			UCTemperatureChart uCTemperatureChart = sender as UCTemperatureChart;
			if (uCTemperatureChart != null)
			{
				uCTemperatureChart.UpdataTemperatureSeries();
			}
		}

		private static void OnUseHotBonnetChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			UCTemperatureChart uCTemperatureChart = sender as UCTemperatureChart;
			if (uCTemperatureChart != null)
			{
				uCTemperatureChart.UpdataHotBonnetSeries();
			}
		}

		private static void OnExperimentChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			UCTemperatureChart uCTemperatureChart = sender as UCTemperatureChart;
			if (uCTemperatureChart != null)
			{
				uCTemperatureChart.UpdataTemperatureChart();
			}
		}

		private void CalInterval()
		{
			if (Experiment.Device.RunTemperatureList.Count >= 2)
			{
				double drawLayoutWidth = Chart.GetDrawLayoutWidth();
				double num = Chart.GetCurrentMaxX() - Chart.GetCurrentMinX();
				double totalSeconds = (Experiment.Device.RunTemperatureList[1].TimeRecord - Experiment.Device.RunTemperatureList[0].TimeRecord).TotalSeconds;
				interval = (int)Math.Ceiling(num * singlePix / (drawLayoutWidth * totalSeconds));
				if (interval < 1)
				{
					interval = 1;
				}
			}
		}

		public UCTemperatureChart()
		{
			InitializeComponent();
			Chart.SetTitleX(Utility.GetMessage("Time"));
			Chart.SetTitleY(Utility.GetMessage("Temperature"));
			Chart.SetYFormat("0.00");
			Chart.MinY = 0.0;
			Chart.MaxY = 110.0;
			Chart.MinX = 0.0;
			Chart.MaxX = 300.0;
			_HotlidTemperatureSeries.PEs.Add(new PaintElement());
			_HotlidTemperatureSeries.PEs[0].Fill = Utility.GetDrawingColor("ColorHotlidTemperature");
			Chart.AddDataSeries(_HotlidTemperatureSeries);
			_BlockTemperatureSeries.PEs.Add(new PaintElement());
			_BlockTemperatureSeries.PEs[0].Fill = Utility.GetDrawingColor("ColorBlockTemperature");
			Chart.AddDataSeries(_BlockTemperatureSeries);
			_TubeTemperatureSeries.PEs.Add(new PaintElement());
			_TubeTemperatureSeries.PEs[0].Fill = Utility.GetDrawingColor("ColorTubeTemperature");
			_TubeTemperatureSeries.Visible = false;
			Chart.AddDataSeries(_TubeTemperatureSeries);
			for (int i = 0; i < _SubTubeTemperatureSeries.Count(); i++)
			{
				_SubTubeTemperatureSeries[i] = new XYSeriesEx();
				_SubTubeTemperatureSeries[i].PEs.Add(new PaintElement());
				_SubTubeTemperatureSeries[i].PEs[0].Fill = GetSubColor(i);
				_SubTubeTemperatureSeries[i].Visible = false;
				Chart.AddDataSeries(_SubTubeTemperatureSeries[i]);
			}
			Chart.OnAxixRangeChanged += Chart_OnAxixRangeChanged;
			Chart.Chart.SizeChanged += Chart_SizeChanged;
		}

		private Color GetSubColor(int index)
		{
			switch (index)
			{
			case 0:
				return Color.Red;
			case 1:
				return Color.Orange;
			case 2:
				return Color.Yellow;
			case 3:
				return Color.Khaki;
			case 4:
				return Color.Green;
			case 5:
				return Color.Purple;
			case 6:
				return Color.Pink;
			default:
				return Color.Brown;
			}
		}

		public void ShowHotlidBlockTubeSeries(bool visible = true)
		{
			_HotlidTemperatureSeries.Visible = visible && (Experiment.UseHotBonnet || ConfigReader.GetInstance().GetShowHotlidTemperature());
			_BlockTemperatureSeries.Visible = visible && (Experiment.TemperatureControlMode != ETemperatureControlMode.Fast || ConfigReader.GetInstance().GetShowHotlidTemperature());
			_TubeTemperatureSeries.Visible = visible && (Experiment.TemperatureControlMode == ETemperatureControlMode.Tube || Experiment.TemperatureControlMode == ETemperatureControlMode.Fast || ConfigReader.GetInstance().GetShowHotlidTemperature());
		}

		public void ShowSubBlockTemperature(int index, bool visible = true)
		{
			if (index >= 0 && index < _SubTubeTemperatureSeries.Count())
			{
				_SubTubeTemperatureSeries[index].Visible = visible;
			}
		}

		public void Init()
		{
			RefreshSeries();
		}

		public void Dispose()
		{
			Chart.Dispose();
			windowsFormsHost.Dispose();
		}

		private void UpdataHotBonnetSeries()
		{
			_HotlidTemperatureSeries.Visible = UseHotBonnet || ConfigReader.GetInstance().GetShowHotlidTemperature();
		}

		private void UpdataTemperatureSeries()
		{
			switch (TemperatureControlMode)
			{
			case ETemperatureControlMode.Module:
				_BlockTemperatureSeries.Visible = true;
				_TubeTemperatureSeries.Visible = ConfigReader.GetInstance().GetShowHotlidTemperature();
				break;
			case ETemperatureControlMode.Tube:
				_BlockTemperatureSeries.Visible = true;
				_TubeTemperatureSeries.Visible = true;
				break;
			case ETemperatureControlMode.Fast:
				_BlockTemperatureSeries.Visible = ConfigReader.GetInstance().GetShowHotlidTemperature();
				_TubeTemperatureSeries.Visible = true;
				break;
			}
		}

		public void RefreshSeries()
		{
			if (!AccuracyDown)
			{
				_HotlidTemperatureSeries.Points.Clear();
				_BlockTemperatureSeries.Points.Clear();
				_TubeTemperatureSeries.Points.Clear();
				XYSeriesEx[] subTubeTemperatureSeries = _SubTubeTemperatureSeries;
				foreach (XYSeriesEx xYSeriesEx in subTubeTemperatureSeries)
				{
					xYSeriesEx.Points.Clear();
				}
				foreach (RunTemperature runTemperature2 in Experiment.Device.RunTemperatureList)
				{
					_HotlidTemperatureSeries.Points.Add(new XYDataPoint(runTemperature2.TimeRecord.TotalSeconds, runTemperature2.HotlidTemperature, "", false));
					_BlockTemperatureSeries.Points.Add(new XYDataPoint(runTemperature2.TimeRecord.TotalSeconds, runTemperature2.BlockTemperature, "", false));
					_TubeTemperatureSeries.Points.Add(new XYDataPoint(runTemperature2.TimeRecord.TotalSeconds, runTemperature2.TubeTemperature, "", false));
					for (int j = 0; j < _SubTubeTemperatureSeries.Count(); j++)
					{
						_SubTubeTemperatureSeries[j].Points.Add(new XYDataPoint(runTemperature2.TimeRecord.TotalSeconds, runTemperature2.SubBlockTemperture[j], "", false));
					}
				}
			}
			RunTemperature runTemperature = Experiment.Device.RunTemperatureList.OrderByDescending((RunTemperature s) => s.TimeRecord).FirstOrDefault();
			if (runTemperature != null && runTemperature.TimeRecord.TotalSeconds > Chart.GetCurrentMaxX())
			{
				Chart.MaxX = runTemperature.TimeRecord.TotalSeconds;
				scrollBar.Maximum = runTemperature.TimeRecord.TotalSeconds - 300.0;
				return;
			}
			Chart.MinX = 0.0;
			Chart.MaxX = 300.0;
			scrollBar.Minimum = 0.0;
			scrollBar.Value = 0.0;
			scrollBar.Maximum = 0.0;
		}

		private void UpdataTemperatureChart()
		{
			if (Experiment != null)
			{
				RefreshSeries();
				Experiment.Device.RunTemperatureUpdateEvent += Device_RunTemperatureUpdateEvent;
			}
		}

		private void Chart_OnAxixRangeChanged(object sender, EventArgs e)
		{
			ReloadPoint();
		}

		private void Chart_SizeChanged(object sender, EventArgs e)
		{
			ReloadPoint();
		}

		private void ReloadPoint()
		{
			if (!AccuracyDown)
			{
				return;
			}
			CalInterval();
			if (Experiment.Device.RunTemperatureList.Count == 0)
			{
				_HotlidTemperatureSeries.Points.Clear();
				_BlockTemperatureSeries.Points.Clear();
				_TubeTemperatureSeries.Points.Clear();
				XYSeriesEx[] subTubeTemperatureSeries = _SubTubeTemperatureSeries;
				foreach (XYSeriesEx xYSeriesEx in subTubeTemperatureSeries)
				{
					xYSeriesEx.Points.Clear();
				}
				return;
			}
			int num = 0;
			for (int j = 0; j < Experiment.Device.RunTemperatureList.Count(); j += interval)
			{
				if ((j + interval < Experiment.Device.RunTemperatureList.Count && Experiment.Device.RunTemperatureList[j].TimeRecord.TotalSeconds < Chart.GetCurrentMinX() && Experiment.Device.RunTemperatureList[j + interval].TimeRecord.TotalSeconds < Chart.GetCurrentMinX()) || (j + interval < Experiment.Device.RunTemperatureList.Count && Experiment.Device.RunTemperatureList[j].TimeRecord.TotalSeconds > Chart.GetCurrentMaxX() && Experiment.Device.RunTemperatureList[j + interval].TimeRecord.TotalSeconds > Chart.GetCurrentMaxX()))
				{
					continue;
				}
				XYDataPointCollection points = _HotlidTemperatureSeries.Points;
				XYDataPointCollection points2 = _BlockTemperatureSeries.Points;
				XYDataPointCollection points3 = _TubeTemperatureSeries.Points;
				if (num < points.Count)
				{
					points[num].ValueX = Experiment.Device.RunTemperatureList[j].TimeRecord.TotalSeconds;
					points[num].ValueY = Experiment.Device.RunTemperatureList[j].HotlidTemperature;
					points2[num].ValueX = Experiment.Device.RunTemperatureList[j].TimeRecord.TotalSeconds;
					points2[num].ValueY = Experiment.Device.RunTemperatureList[j].BlockTemperature;
					points3[num].ValueX = Experiment.Device.RunTemperatureList[j].TimeRecord.TotalSeconds;
					points3[num].ValueY = Experiment.Device.RunTemperatureList[j].TubeTemperature;
					for (int k = 0; k < _SubTubeTemperatureSeries.Count(); k++)
					{
						_SubTubeTemperatureSeries[k].Points[num].ValueX = Experiment.Device.RunTemperatureList[j].TimeRecord.TotalSeconds;
						_SubTubeTemperatureSeries[k].Points[num].ValueY = Experiment.Device.RunTemperatureList[j].SubBlockTemperture[k];
					}
				}
				else
				{
					points.Add(new XYDataPoint(Experiment.Device.RunTemperatureList[j].TimeRecord.TotalSeconds, Experiment.Device.RunTemperatureList[j].HotlidTemperature, string.Empty, false));
					points2.Add(new XYDataPoint(Experiment.Device.RunTemperatureList[j].TimeRecord.TotalSeconds, Experiment.Device.RunTemperatureList[j].BlockTemperature, string.Empty, false));
					points3.Add(new XYDataPoint(Experiment.Device.RunTemperatureList[j].TimeRecord.TotalSeconds, Experiment.Device.RunTemperatureList[j].TubeTemperature, string.Empty, false));
					for (int l = 0; l < _SubTubeTemperatureSeries.Count(); l++)
					{
						_SubTubeTemperatureSeries[l].Points.Add(new XYDataPoint(Experiment.Device.RunTemperatureList[j].TimeRecord.TotalSeconds, Experiment.Device.RunTemperatureList[j].SubBlockTemperture[l], string.Empty, false));
					}
				}
				num++;
			}
			for (int num2 = _HotlidTemperatureSeries.Points.Count - 1; num2 >= num; num2--)
			{
				_HotlidTemperatureSeries.Points.RemoveAt(num2);
				_BlockTemperatureSeries.Points.RemoveAt(num2);
				_TubeTemperatureSeries.Points.RemoveAt(num2);
				for (int m = 0; m < _SubTubeTemperatureSeries.Count(); m++)
				{
					_SubTubeTemperatureSeries[m].Points.RemoveAt(num2);
				}
			}
			_HotlidTemperatureSeries.Points.Add(new XYDataPoint(Experiment.Device.RunTemperatureList[Experiment.Device.RunTemperatureList.Count - 1].TimeRecord.TotalSeconds, Experiment.Device.RunTemperatureList[Experiment.Device.RunTemperatureList.Count - 1].HotlidTemperature, string.Empty, false));
			_BlockTemperatureSeries.Points.Add(new XYDataPoint(Experiment.Device.RunTemperatureList[Experiment.Device.RunTemperatureList.Count - 1].TimeRecord.TotalSeconds, Experiment.Device.RunTemperatureList[Experiment.Device.RunTemperatureList.Count - 1].BlockTemperature, string.Empty, false));
			_TubeTemperatureSeries.Points.Add(new XYDataPoint(Experiment.Device.RunTemperatureList[Experiment.Device.RunTemperatureList.Count - 1].TimeRecord.TotalSeconds, Experiment.Device.RunTemperatureList[Experiment.Device.RunTemperatureList.Count - 1].TubeTemperature, string.Empty, false));
			for (int n = 0; n < _SubTubeTemperatureSeries.Count(); n++)
			{
				_SubTubeTemperatureSeries[n].Points.Add(new XYDataPoint(Experiment.Device.RunTemperatureList[Experiment.Device.RunTemperatureList.Count - 1].TimeRecord.TotalSeconds, Experiment.Device.RunTemperatureList[Experiment.Device.RunTemperatureList.Count - 1].SubBlockTemperture[n], string.Empty, false));
			}
		}

		private void Device_RunTemperatureUpdateEvent(object sender, EventArgs e)
		{
			if (!(e is RunTemperatureEventArgs))
			{
				return;
			}
			RunTemperature runTemperature = ((RunTemperatureEventArgs)e).RunTemperature;
			if (runTemperature == null)
			{
				return;
			}
			if (!AccuracyDown)
			{
				_HotlidTemperatureSeries.Points.Add(new XYDataPoint(runTemperature.TimeRecord.TotalSeconds, runTemperature.HotlidTemperature, "", false));
				_BlockTemperatureSeries.Points.Add(new XYDataPoint(runTemperature.TimeRecord.TotalSeconds, runTemperature.BlockTemperature, "", false));
				_TubeTemperatureSeries.Points.Add(new XYDataPoint(runTemperature.TimeRecord.TotalSeconds, runTemperature.TubeTemperature, "", false));
				for (int i = 0; i < _SubTubeTemperatureSeries.Count(); i++)
				{
					_SubTubeTemperatureSeries[i].Points.Add(new XYDataPoint(runTemperature.TimeRecord.TotalSeconds, runTemperature.SubBlockTemperture[i], string.Empty, false));
				}
			}
			else
			{
				CalInterval();
				if (_HotlidTemperatureSeries.Points.Count == 0 || Experiment.Device.RunTemperatureList.Count < 2 || Experiment.Device.RunTemperatureList.Count % interval == 0)
				{
					_HotlidTemperatureSeries.Points.Add(new XYDataPoint(runTemperature.TimeRecord.TotalSeconds, runTemperature.HotlidTemperature, "", false));
					_BlockTemperatureSeries.Points.Add(new XYDataPoint(runTemperature.TimeRecord.TotalSeconds, runTemperature.BlockTemperature, "", false));
					_TubeTemperatureSeries.Points.Add(new XYDataPoint(runTemperature.TimeRecord.TotalSeconds, runTemperature.TubeTemperature, "", false));
					for (int j = 0; j < _SubTubeTemperatureSeries.Count(); j++)
					{
						_SubTubeTemperatureSeries[j].Points.Add(new XYDataPoint(runTemperature.TimeRecord.TotalSeconds, runTemperature.SubBlockTemperture[j], string.Empty, false));
					}
				}
				else
				{
					_HotlidTemperatureSeries.Points[_HotlidTemperatureSeries.Points.Count - 1].ValueX = runTemperature.TimeRecord.TotalSeconds;
					_HotlidTemperatureSeries.Points[_HotlidTemperatureSeries.Points.Count - 1].ValueY = runTemperature.HotlidTemperature;
					_BlockTemperatureSeries.Points[_BlockTemperatureSeries.Points.Count - 1].ValueX = runTemperature.TimeRecord.TotalSeconds;
					_BlockTemperatureSeries.Points[_BlockTemperatureSeries.Points.Count - 1].ValueY = runTemperature.BlockTemperature;
					_TubeTemperatureSeries.Points[_TubeTemperatureSeries.Points.Count - 1].ValueX = runTemperature.TimeRecord.TotalSeconds;
					_TubeTemperatureSeries.Points[_TubeTemperatureSeries.Points.Count - 1].ValueY = runTemperature.TubeTemperature;
					for (int k = 0; k < _SubTubeTemperatureSeries.Count(); k++)
					{
						_SubTubeTemperatureSeries[k].Points[_HotlidTemperatureSeries.Points.Count - 1].ValueX = runTemperature.TimeRecord.TotalSeconds;
						_SubTubeTemperatureSeries[k].Points[_HotlidTemperatureSeries.Points.Count - 1].ValueY = runTemperature.SubBlockTemperture[k];
					}
				}
			}
			if (!UpdateMaxX || !(runTemperature.TimeRecord.TotalSeconds > scrollBar.Maximum + 300.0))
			{
				return;
			}
			if (scrollBar.Value == scrollBar.Maximum)
			{
				scrollBar.Value += 300.0;
				if (!double.IsNaN(Chart.GetCurrentMaxX()) && !double.IsNaN(Chart.GetCurrentMinX()))
				{
					Chart.MaxX = Chart.GetCurrentMaxX() + 300.0;
					Chart.MinX = Chart.GetCurrentMinX() + 300.0;
				}
			}
			scrollBar.Maximum += 300.0;
		}

		private void scrollBar_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
		{
			if (scrollBar.Visibility == Visibility.Visible)
			{
				double value = scrollBar.Value;
				double maxX = value + 300.0;
				if (value >= Chart.GetCurrentMaxX())
				{
					Chart.MaxX = maxX;
					Chart.MinX = value;
				}
				else
				{
					Chart.MinX = value;
					Chart.MaxX = maxX;
				}
			}
		}

		public void DisplayAllData()
		{
			if (Experiment.Device.RunTemperatureList != null && Experiment.Device.RunTemperatureList.Count >= 2)
			{
				Chart.MinX = Experiment.Device.RunTemperatureList[0].TimeRecord.TotalSeconds;
				Chart.MaxX = Experiment.Device.RunTemperatureList[Experiment.Device.RunTemperatureList.Count - 1].TimeRecord.TotalSeconds;
				scrollBar.Maximum = scrollBar.Minimum;
			}
			scrollBar.Visibility = Visibility.Collapsed;
		}
	}
}
